package icu.groot.alan.dao.impl;

import icu.groot.alan.dao.AnnounceDao;
import icu.groot.alan.entity.Announce;
import icu.groot.alan.util.Dao;

import javax.naming.ldap.PagedResultsControl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AnnounceDaoImpl implements AnnounceDao {
    @Override
    public List<Announce> getAllAnnounceTask(Connection connection) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Announce> announces = new ArrayList<>();
        Announce announce = null;
        Object[] params = {};
        String sql = "select * from announce left join task on announce.task_code = task.task_code";
        resultSet = Dao.execute(connection, preparedStatement, resultSet, sql, params);
        while (resultSet.next()) {
            announce = new Announce();
            announce.setId(resultSet.getInt("id"));
            announce.setTaskName(resultSet.getString("task_name"));
            announce.setTaskCode(resultSet.getInt("task_code"));
            announce.setStartTimeStamp(resultSet.getInt("start_time"));
            announce.setEndTimeStamp(resultSet.getInt("end_time"));
            announces.add(announce);
        }
        Dao.closeResource(preparedStatement,resultSet);
        return announces;
    }

    @Override
    public List<Announce> getNeedComplete(Connection connection, long nowTime) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Announce> announces = new ArrayList<>();
        Announce announce = null;
        Object[] params = {nowTime};
        String sql = "SELECT a.id announceId, t.task_name taskName, a.start_time startTime, a.end_time endTime, a.task_code taskCode\n" +
                " FROM announce a LEFT JOIN task t ON a.task_code = t.task_code WHERE a.end_time > ? ";
        resultSet = Dao.execute(connection, preparedStatement, resultSet, sql, params);
        while (resultSet.next()) {
            announce = new Announce();
            announce.setId(resultSet.getInt("announceId"));
            announce.setTaskCode(resultSet.getInt("taskCode"));
            announce.setTaskName(resultSet.getString("taskName"));
            announce.setStartTimeStamp(resultSet.getInt("startTime"));
            announce.setEndTimeStamp(resultSet.getInt("endTime"));
            announces.add(announce);
        }
        Dao.closeResource(preparedStatement, resultSet);
        return announces;
    }

    @Override
    public int accounceTask(Connection connection, int taskCode, int startTime, int endTime) throws SQLException {
        PreparedStatement preparedStatement = null;
        Object[] params = {taskCode, startTime, endTime};
        String sql = "insert into announce (task_code, start_time, end_time) values (?, ?, ?)";
        int updateRow = Dao.execute(connection, preparedStatement, sql, params);
        Dao.closeResource(preparedStatement);
        return updateRow;
    }

    @Override
    public List<Integer> getAlreadyReportAnnounceIds(Connection connection, long endTime, int userCode) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Object[] params = {endTime, userCode};
        List<Integer> ids = new ArrayList<>();
        String sql = "SELECT announce_id id FROM report LEFT JOIN announce ON report.announce_id = announce.id WHERE announce.end_time > ? AND report.user_code = ?";
        resultSet = Dao.execute(connection, preparedStatement, resultSet, sql, params);
        while (resultSet.next()) {
            ids.add(resultSet.getInt("id"));
        }
        Dao.closeResource(preparedStatement, resultSet);
        return ids;

    }
}
